package com.yq.interview.study.thread;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName AtomicReferenceDemo
 * @Description TODO
 * @Author yaoqiang
 * @Date 2020/11/18 9:50
 * @Version 1.0
 */
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User z3=new User("z3",22);
        User l4=new User("l4",28);
        AtomicReference<User> atomicReference=new AtomicReference<>();
        atomicReference.set(z3);
        System.out.println(atomicReference.compareAndSet(z3,l4)+"\t"+atomicReference.get().toString());

        System.out.println(atomicReference.compareAndSet(z3,l4)+"\t"+atomicReference.get().toString());
    }
}

class User{
    String userName;
    int age;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }

    public User() {
    }

    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }
}